package com.pro_one.mobile.controller;

import com.aliyuncs.exceptions.ClientException;
import com.pro_one.constant.MessageConstant;
import com.pro_one.constant.RedisConstant;
import com.pro_one.entity.Result;
import com.pro_one.pojo.*;
import com.pro_one.service.OrderService;
import com.pro_one.service.OrderSettingService;
import com.pro_one.util.SMSUtils;
import com.pro_one.util.ValidateCodeUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 提交预约信息
     *
     * @param map
     * @return
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map, Integer orderaddressId) {
        System.out.println(orderaddressId);
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        String validateCodeInRedis = jedisPool.getResource().get(telephone + RedisConstant.SENDTYPE_ORDER);
        String telephoneInRedis = jedisPool.getResource().get(telephone);
        if (validateCodeInRedis == null || validateCode == null
                || !validateCode.equals(validateCodeInRedis) || !telephone.equals(telephoneInRedis)) {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = null;
        try {
            result = orderService.order(map,orderaddressId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
        if (result.isFlag()) {
            try {
                jedisPool.getResource().del(telephone);
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE, telephone, (String) map.get("orderDate"));
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 展示预约成功后信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }

    /**
     * 根据预约日期查询预约设置信息
     *
     * @param orderDate
     * @return
     */
    @RequestMapping("/findOrdersettingByOrderDate")
    public Result findOrdersettingByOrderDate(String orderDate) {
        try {
            OrderSetting orderSetting = orderSettingService.findOrdersettingByOrderDate(orderDate);
            if (orderSetting == null) {
                return new Result(false, "当日未设置预约信息，请选择其他日期");
            }
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }
    /*查询所有的预约套餐*/
    @RequestMapping("getSetmeal")
    public Result getOrderAndSetmeal(String telephone){
        try {
      List<Setmeal> list=orderService.getOrderAndSetmeal(telephone);
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    /*健康干预*/
    @RequestMapping("findByIdorder")
    public Result findByIdOrder(Integer id){

        try {
          OrderFitness order= orderService.findByIdOrder(id);
          return new Result(true,MessageConstant.SELECT_OEDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SELECT_ORDER_FAIL);
        }
    }

}
